const api = require("../../../../utils/cloudAPIUtils");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:app.globalData.title,
    deviceSn:null,
    devInfo:{},
    agreement:{
        checked:true
    }
  },

  _loadDeviceData:function(){
    api.post(api.url.pclScan,{
        deviceSn:this.data.deviceSn
    },res=>{
        if(res.data.code==200){
            if(res.data.body.tradeNo){
                wx.redirectTo({
                    url: '../result/result?tradeNo='+res.data.body.tradeNo,
                })
            }else{
                if(res.data.body.templateId){
                    this.setData({
                        devInfo:res.data.body
                    })
                }else{
                    wx.showModal({
                        content: '未配置套餐，请联系客服',
                        showCancel:false,
                        success(){
                            wx.reLaunch({
                                url: '/pages/index/index',
                            })
                        }
                    })
                }
            }
        }else if(res.data.msg){
            wx.showModal({
              content: res.data.msg,
              showCancel:false,
              success(){
                  wx.reLaunch({
                    url: '/pages/index/index',
                  })
              }
            })
        }else{
            wx.showToast({
              title: '获取信息失败',
              icon:'error'
            })
            setTimeout(function(){
                wx.reLaunch({
                    url: '/pages/index/index',
                })
            },1000);
        }
    })
  },

  //勾选授权
  bindChangeAgreement(){
    const agreement = this.data.agreement;
    agreement.checked = !agreement.checked;
    this.setData({agreement});
  },

  btnPay:function(e){
    let index = e.currentTarget.dataset.index;
    let setMealItem = this.data.devInfo.freeItems[index];

    if(!this.data.agreement.checked){
        wx.showModal({
          title: '温馨提示',
          content: '请先勾选同意授权书',
          showCancel:false,
        })
        return
    }
    if(!setMealItem){
        wx.showModal({
            title: '温馨提示',
            content: '请选择一个套餐使用',
            showCancel:false,
          })
          return
    }
    const self = this;
    api.post(api.url.pclPay,{
        payType:3,
        appId:wx.getAccountInfoSync().miniProgram.appId,
        deviceSn:this.data.deviceSn,
        templateId:this.data.devInfo.templateId,
        setMealId:setMealItem.id
    },(res)=>{
        console.log(res)
        if(res.data.code==200){
            const tradeNo = res.data.body.tradeNo;

            const payInfo = JSON.parse(res.data.body.payInfo);
            wx.requestPayment({
                appId:payInfo.appId,
                nonceStr: payInfo.nonceStr,
                package: payInfo.package,
                paySign: payInfo.sign,
                timeStamp: payInfo.timeStamp+"",
                signType:payInfo.signType,
                success(res){
                    console.log(res)
                    self.toUnderway(tradeNo);
                },fail(err){
                    wx.showToast({
                        title: '支付失败',
                        icon:'error',
                        mask:true
                    })
                }
            })
        }else if(res.data.msg){
            wx.showModal({
                content: res.data.msg,
                showCancel:false
            })
        }else{
            wx.showToast({
              title: '支付失败',
              icon:'error'
            })
        }
    })
  },

  toUnderway(tradeNo){
    wx.redirectTo({
      url: '/packageA/pages/pcl/result/result?tradeNo='+tradeNo,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.deviceSn){
        this.data.deviceSn = options.deviceSn;
        this._loadDeviceData();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})